/**
 * 待定义
 * 基本上这里要放页面中会用到的东西，如时间处理，账号操作confirm
 */
import {
	formatSeconds,
	getFormatDuringTime, msgboxLayer,
	msgInterface,
	timestampToDate,
	timestaptodate,
	toNonExponential,
} from '@/libs/site.utils'


//导出
export const mixinsUtils =  {
	data () {
		return {
		
		}
	},
	computed:{
		//除以10的X次幂，或者说要消几个0, 可能没用到，但保留着
		precisionXgToNum(value,precision=8){
			return (value, precision)=>{
				return parseFloat(value) / Math.pow(10, precision)
			}
		}
	},
	created () {

	},
	methods: {
		/**
		 * 智能合约广播回调
		 * @param ACTION 操作标题
		 * @param ACTION_TITLE 操作详情
		 * @param extraCallback 额外的业务处理
		 * @returns {function(*=, *=): void}
		 */
		smartContractBroadcastCallback(ACTION,ACTION_TITLE,extraCallback){
			return (status, txid) => {
				if (status === 0) { //失敗
					this.MsgInterface(ACTION, '', -1, '', ACTION_TITLE);
				} else if (status === 1) { //成功
					this.MsgInterface(ACTION, '', 2, 'https://tronscan.io/#/transaction/' + txid, ACTION_TITLE);
				} else if (status === -1) { //取消
					this.MsgInterface(ACTION, '', 3, '', ACTION_TITLE);
				}
				extraCallback(status,txid)
			}
		},
		
		/**
		 * 触发智能合约的最终交易确认回调
		 * @param ACTION 动作
		 * @param ACTION_TITLE 动作详情
		 * @returns {function(*, *): void}
		 */
		smartContractConfirmCallback(ACTION,ACTION_TITLE){
			return  (txid, status) => {//区块链结果查询回调的信息，应该是不影响用户操作的，因为是几秒钟后才显示的
				if (status === "SUCCESS") {
					this.$notify({
						title: this.LangSet(['成功', 'SUCCESS']),
						message: ACTION + ACTION_TITLE,
						type: 'success',
						duration:10000 }
					);
				} else if (status === "OUT_OF_ENERGY") {
					this.$notify.error({
						title:  ACTION + ACTION_TITLE + this.LangSet(['失敗', 'FAIL']),
						message: this.LangSet(['錢包中請至少保留', 'Minimum wallet balance']) + '20TRX',
						duration:10000
					});
				} else {
					this.$notify.error({
						title: this.LangSet(['失敗', 'FAIL']),
						message: ACTION + ACTION_TITLE,
						duration:10000
					});
				}
			}
		},
		
		/**
		 * 鎖定和鎖定的統一提示層入口
		 * @param prefix
		 * @param amount
		 * @param type
		 * @param url
		 * @param token
		 * @constructor
		 */
		MsgInterface (prefix, amount, code, url = '', token=null) {
			let that = this;
			let title = prefix
			amount
				?title = title + amount + token
				:title = title
			let html = '';
			let type = ''//用于alert的类型
			if (code == 1) {
				type = 'info'
				html += '<i class="c-333 f70 fa fa-spinner fa-pulse"></i>';
				html += '<div class="f22 mt10">' + that.LangSet(['等待您的確認', 'Waiting for your confirmation']) + '</div>';
				html += '<div class="f14 mt10">' + that.LangSet(['請在您的錢包中確認', 'Please confirm in your wallet']) + '</div>';
			} else if (code == 2) {
				type = 'success'
				html += '<i class="c-green f100 fa fa-check-circle"></i>';
				html += '<div class="f22 mt10">' + that.LangSet(['交易已提交', 'Deal submited']) + '</div>';
				html += '<div class="f14 mt10"><a href="' + url + '" target="_self" class="c-red">' + that.LangSet([
					'在 TRONSCAN 上查看', 'View on TRONSCAN'
				]) + ' >></a></div>';
				
			} else if (code == 3) {
				type = 'warning'
				html += '<i class="c-red mt20 f100 fa fa-exclamation-circle"></i>';
				html += '<div class="f24 mt20">' + that.LangSet(['交易已取消', 'Deal cancelled']) + '</div>';
			} else if (code == -1) {
				type = 'error'
				html += '<i class="c-red mt20 f100 fa fa-exclamation-circle"></i>';
				html += '<div class="f24 mt20">' + that.LangSet(['交易失敗', 'Deal failed']) + '</div>';
			} else if (code == 0) {
				type = 'error'
				html += '<i class="c-red mt20 f100 fa fa-exclamation-circle"></i>';
				html += '<div class="f24 mt20">' + that.LangSet(['錢包中請保留20TRX，確保交易順暢',
					'Please keep 20trx in your wallet to ensure smooth transaction'
				]) + '</div>';
			}
			msgboxLayer(title, html)
		},
		
	},
	filters:{

		//格式化金錢,沒有針對小數的
		money (t) {
			return (String(t)).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
		},

		//格式化金錢，有處理小數點的
		money2 (t) {
			t = (String(t)).split('.');
			//處理沒有小數時候的顯示
			let decimalStr = t[1] === undefined ? '' : '.' + t[1];
			return t[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',') + decimalStr;
		},

		//token縮減
		token (t, splic = 4) {

			return t ? t.slice(0, splic) + '...' + t.slice(-splic) : ''
		},

		num (t) {
			return toNonExponential(new Number(new Number(t).toFixed(3)));
		},

		//數據縮小統計
		count (t) {
			var tstr = String(t); //默認為字符串

			t = (Number(t)); //轉為數字

			if (t >= 10000000) {
				// return (t/1000000) + 'M'
				return tstr.slice(0, tstr.length - 6) + 'M'
			} else if (t == 0) {
				return '暫無'
			} else if (t >= 10 && t < 1000) {
				//return (t/1000).toFixed(2) + 'K'
				return t.toFixed(2);
			} else if (t < 10 && t >= 1) {
				return toNonExponential(new Number(t.toFixed(4)));
			} else if (t < 1 && t > 0) {
				return toNonExponential(new Number(t.toFixed(6)));
			} else {
				//注意，這裡會四捨五入
				return (t / 1000).toFixed(2) + 'K'
				//return tstr.slice(0,tstr.length-3) + 'K' //先除1000再轉為字符串，才能使用slice方法
			}
		},


		//秒數轉剩餘時間，如果是中文，就使用聪明显示方法
		tt (t) {

			return formatSeconds(t)
			//注意这里的this.lp,无法得到this,参考：https://www.zhihu.com/question/267105536
			/*if (this.lp === "0") {
				return getFormatDuringTime(t); //聪明的时间显示
			} else {
				return formatSeconds(t) // 沒有天的
			}*/
		},
		ttp (t,lp){
			if (lp === "0"||lp===0) {
				return getFormatDuringTime(t); //聪明的时间显示
			} else {
				return formatSeconds(t) // 沒有天的
			}
		},

		//時間戳轉時間
		td (t) {
			return timestaptodate(t * 1000); //方法是使用13位的，所以要*1000
		},
		//时间戳转时间，按格式
		ft (t, fmt) {
			return timestampToDate(t * 1000, fmt);
		},
		//精度处理
		tofix (value, precision) {
			let v = parseFloat(value);
			if (!isNaN(v)) {
				value = v.toFixed(precision);

			}
			return value;
		},//


	}
}
